﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
var loaibaocao = -1;
var tringdoId = -1;
var vuId = -1;
var col = "";
var loaiexport = -1;
var id = -1;
app.controller('DemoCtrl', [
    '$scope', '$http',
    'AppDialogService',
function ($scope, $http, AppDialogService) {
    'use strict';
    /*
    $http.get(Editor.Url + "/pq/GetAll")
     .success(function (response) {
         $scope.tasks = response;
         console.dir(response);
     });
    */
    $("#bctd").hide();
    $("#bcvu").hide();
    $("#exportid").hide();
    loaibaocao = -1;
    tringdoId = -1;
    vuId = -1;
    col = "";
    loaiexport = -1;
    id = -1;
    $scope.loadDataTrinhDo = function (id) {
        var params = {
            tdid: id
        }

        $http.post(Editor.Url + "/Report/ReportTrinhDo", params)
                .success(function (response) {
                    $scope.orderByField = 'tENNV';
                    $scope.reverseSort = true;
                    $scope.nhanviens = response;
                    $("#exportid").hide();
                    if (response.length > 0) {
                        $("#exportid").show();
                    }
                });
    }

    $scope.loadDataVu = function (id) {
        var params = {
            cvid: id
        }

        $http.post(Editor.Url + "/Report/ReportVu", params)
                .success(function (response) {
                    $scope.orderByField = 'tENNV';
                    $scope.reverseSort = true;
                    $scope.nhanviens = response;
                    $("#exportid").hide();
                    if (response.length > 0) {
                        $("#exportid").show();
                    }
                });
    }
    $scope.Ngaysinhs = function (id) {        
        var _ngaysinh = "ReportDuoi30t";
        if (id === 4) {
            _ngaysinh = "ReportTren30t";
        }
        $http.post(Editor.Url + "/Report/" + _ngaysinh)
                .success(function (response) {
                    $scope.orderByField = 'tENNV';
                    $scope.reverseSort = true;
                    $scope.nhanviens = response;
                    $("#exportid").hide();
                    if (response.length > 0) {
                        $("#exportid").show();
                    }
                });
    }

    //loai bao cáo
    $http.get(url + "/Report/gReportStaff")
       .success(function (response) {
           $scope.reports = response;
       });
    //loai export
    $http.get(url + "/Report/loadiExport")
       .success(function (response) {
           $scope.exports = response;
       });
    // trình độ
    $http.get(url + "/TD/GetAll")
    .success(function (response) {
        $scope.trinhdos = response;
    });



    // bao cáo vụ
    $http.get(url + "/vu/GetVuDetails")
   .success(function (response) {
       $scope.vus = response;
   });

    $scope.selectComboboxTD = function (_id) {
        var k = parseInt(_id, 10);
        id = k;
        $scope.loadDataTrinhDo(k);
    }
    $scope.selectComboboxVu = function (_id) {
        var k = parseInt(_id, 10);
        id = k;
        $scope.loadDataVu(k);
        console.log("vu : " + k);
    }

    $scope.selectComboboxExport = function (exportId) {
        var k = parseInt(exportId,10);
        var params = {
            loaibc: loaibaocao,
            col: "ID,HONV,TENNV,PHAI,NGAYSINH,NOISINH,THANHDANH,DIACHI,NGHENGHIEP,SODIENTHOAI,NHAPMON,CHUYENMON",
            loaiexport: k,
            id :id
        }       
        var params = {
            exportID: k
        }
        $http.post(Editor.Url + "/Report/loaiexport/", params)
               .success(function (response) {         
                   location.href = Editor.Url + "/Report/exportNhanvien";
         });
    }

    $scope.selectCombobox = function (id) {
        $("#bctd").hide();
        $("#bcvu").hide();
        console.log(id);
        var k = parseInt(id, 10);
        loaibaocao = k;
        tringdoId = -1;
        vuId = -1;
        col = "";
        loaiexport = -1;
        switch (k) {
            case 1: {
                $("#bctd").show();
                console.log("tring do");
                break;
            }
            case 2: {
                $("#bcvu").show();
                console.log("vu");
                break;
            }
            case 3: case 4: {
                console.log("Ngay sinh");
                $scope.Ngaysinhs(k);
                break;
            }
        }
    }


    $scope.onclickCheckBox = function (_item) {
        console.dir(_item);
        if (_item.show === false) {
            _item.edit = false;
            _item.isadd = false;
            _item.isdelete = false;
        }
        else
            if (_item.edit === true || _item.isadd === true || _item.isdelete === true) {
                _item.show = true;
            }
        console.log("demo check box");
        console.dir(_item);
        $http.put(Editor.Url + "/api/PqAPI/" + _item.id, _item).success(function (data, status, headers) {
            if (status === 200) {
            }
        });
    }

    $scope.delete = function (id) {
        var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(Editor.Url + "/api/BanAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }

    $scope.appDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        AppDialogService.openDialog({
            testValue: 'application'
        }).then(function (result) {
            if (result.ok) {
                var mota = $("#mOTABAN").val();
                result.obj.mOTABAN = mota;
                if (flag) {
                    $http.put(Editor.Url + "/api/BanAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                        if (status === 200) {
                            alert("cập nhập thành công");
                        }
                    });
                }
                else {
                    $http.post(Editor.Url + "/api/BanAPI", result.obj).success(function (data, status, headers) {
                        $scope.loadData();
                    });
                }
            }
        });

    };

}]);

app.controller('DialogCtrl', [
    '$scope',
    'AppDialogService',
function ($scope, AppDialogService) {
    'use strict';

    $scope.obj = null;

    $scope.onOpen = function () {
        $scope.obj = item;
        if ($scope.obj != null) {
            $('#mOTABAN').val($scope.obj.mOTABAN).blur();
        }
    };

    $scope.$parent.onOk = function () {
        AppDialogService.closeDialog({ ok: true, obj: $scope.obj });
    };

    $scope.onCancel = function () {
        AppDialogService.closeDialog({ ok: false });
    };

    $scope.beforeClose = function () {
        return "";
    };

}]);